//
//  HomeController.m
//  UI Demo
//
//  Created by Rainy Zou on 15/11/27.
//  Copyright © 2015年 Rainy Zou. All rights reserved.
//

#import "HomeController.h"
#import "SaleReportsController.h"
#import "ProductListController.h"
#import "SuggestController.h"

@interface HomeController ()

@property (nonatomic,strong) NSArray *data;

@end

@implementation HomeController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];
    

    UIImage *image = [UIImage imageNamed:@"home_search1"];
    image = [image stretchableImageWithLeftCapWidth:image.size.width /2     topCapHeight:image.size.height/2];
    UISearchBar *search = [[UISearchBar alloc]init];
    search.frame = CGRectMake(0, 64, self.view.frame.size.width , image.size.height);
    
  //  search.backgroundImage = image;
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    
    CGFloat margin = 0;
    
    CGFloat width = self.view.frame.size.width-2*margin;
    CGFloat height = 175;
    
    for (int i = 0; i<5; i++) {
        UIImageView *view = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"image%d",i+1]]];
        view.frame = CGRectMake(width*i, margin, width, height );
        [scrollView addSubview:view];
    }
    
    scrollView.contentSize = CGSizeMake(width*5, height);
    
    scrollView.frame = CGRectMake(margin, CGRectGetMaxY(search.frame), self.view.frame.size.width - 2*margin, height);
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    
    
    UITableView *tableView =  [[UITableView alloc]init];
    
    tableView.frame = CGRectMake(0, CGRectGetMaxY(scrollView.frame), self.view.frame.size.width, 300);
    tableView.dataSource = self;
    tableView.delegate = self;
    
    
    [self.view addSubview:search];
    [self.view addSubview:scrollView];
    [self.view addSubview:tableView];
    
}

- (void)selectLeftAction:(id) sender
{
    NSLog(@"-----");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(NSArray *)data
{
    if(_data == nil){
        _data = @[@{@"image":@"home_classify_01",@"name":@"限时抢购"},
                  @{@"image":@"home_classify_02",@"name":@"促销快报"},
                  @{@"image":@"home_classify_03",@"name":@"新品上架"},
                  @{@"image":@"home_classify_04",@"name":@"热门单品"},
                  @{@"image":@"home_classify_05",@"name":@"推荐品牌"}];
    }
    return _data;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static  NSString *reuseId = @"homecell";
    
    NSDictionary *dict = self.data[indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
    }
    NSString *imageStr = dict[@"image"];
    cell.imageView.image =[UIImage imageNamed:imageStr];
    cell.textLabel.text = dict[@"name"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"select -- ");
    switch (indexPath.row) {
        case 1:
        {
            SaleReportsController *controller  = [[SaleReportsController alloc]init];
            [self.navigationController pushViewController:controller animated:YES];
            break;
        }
        case 2:
        {
            ProductListController *controller = [[ProductListController alloc]init];
            [self.navigationController pushViewController:controller animated:YES];
            break;
        }
            case 4 :
        {
            SuggestController *controller = [[SuggestController alloc]init];
            [self.navigationController pushViewController:controller animated:YES];
            break;
        }
            
        default:
            break;
    }
    
}

@end
